package systemClasses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class User {
	
	private String login;
	private String name;
	private String address;
	private String iD;
	private int reputation;
	
	private ArrayList<Item> myRegisteredItems;
	private ArrayList<User> friendshipRequests;
	private ArrayList<User> myFriends;
	private ArrayList<Item> myBorrowedItems; //the user borrowed the items
	private ArrayList<Item> myItemsBorrowed; //items that the user borrowed
	private ArrayList<Item> myRequestedItems;
	private ArrayList<Item> myReturnedItems; //need to be analyzed
	private ArrayList<Item> itemsReturnedToOwner; //need to be analyzed
	private ArrayList<Message> negotiationMessages;
	private ArrayList<Message> offTopicMessages;
	private ArrayList<Message> allMessages;
	private Map<Item, ArrayList<User>> desiredItems;
	private ArrayList<Item> myReturnRequests;
	private ArrayList<String> myHistoric;
	
	public User(String login, String name, String address) throws Exception{
		setLogin(login);
		setName(name);
		setAddress(address);
		myRegisteredItems = new ArrayList<Item>();
		friendshipRequests = new ArrayList<User>();
		myFriends = new ArrayList<User>();
		myBorrowedItems = new ArrayList<Item>();
		myItemsBorrowed = new ArrayList<Item>();
		myRequestedItems = new ArrayList<Item>();
		myReturnedItems = new ArrayList<Item>();
		itemsReturnedToOwner = new ArrayList<Item>();
		negotiationMessages = new ArrayList<Message>();
		offTopicMessages = new ArrayList<Message>();
		allMessages = new ArrayList<Message>();
		desiredItems = new HashMap<Item, ArrayList<User>>();
		myReturnRequests = new ArrayList<Item>();
		myHistoric = new ArrayList<String>();
	}
	
	public void setLogin(String login) throws Exception{
		if(login == null || login.equals(""))
			throw new Exception("Login inválido");
		
		this.login = login;
	}
	
	public void setName(String name) throws Exception{
		if(name == null || name.equals(""))
			throw new Exception("Nome inválido");
		
		this.name = name;
	}
	
	public void setAddress(String address) throws Exception{
		if(address == null || address.equals(""))
			throw new Exception("Endereço inválido");
		
		this.address = address;
	}
	
	public void setID(String iD) throws Exception{
		if(iD == null || iD.equals(""))
			throw new Exception("Identificação inválida");
		
		this.iD = iD;
	}
	
	public void setReputation(int reputation){
		this.reputation = reputation;
	}
	
	public String getLogin(){
		return login;
	}
	
	public String getName(){
		return name;
	}
	
	public String getAddress(){
		return address;
	}
	
	public String getID(){
		return iD;
	}
	
	public int getReputation(){
		return reputation;
	}
	
	public void registerMyItems(Item item) throws Exception{
		if(myRegisteredItems.contains(item))
			throw new Exception("Já existe este item cadastrado");
		
		myRegisteredItems.add(item);
			
	}
	
	public ArrayList<Item> getMyRegisteredItems(){
		return myRegisteredItems;
	}
	
	public ArrayList<User> getFriendshipRequests(){
		return friendshipRequests;
	}
	
	public void addRequestFriendship(User friend) throws Exception{
		if(friend == null)
			throw new Exception("Usuário inválido");
		
		if(friendshipRequests.contains(friend))
			throw new Exception("Requisição já solicitada");
		
		friendshipRequests.add(friend);
	}
	
	public ArrayList<User> getFriends(){
		return myFriends;
	}
	
	public void addFriend(User friend) throws Exception{
		if(friend == null)
			throw new Exception("Usuário inválido");
		
		if(myFriends.contains(friend))
			throw new Exception("Você já é amigo de" + friend.getName());
		
		myFriends.add(friend);
	}
	
	public void addMyBorrowedItems(Item item) throws Exception{
		if(item == null)
			throw new Exception("Item inválido");
		
		if(myBorrowedItems.contains(item))
			throw new Exception("O item já está na lista");
		
		myBorrowedItems.add(item);
	}
	
	public ArrayList<Item> getMyBorrowedItems(){
		return myBorrowedItems;
	}
	
	public void addMyItemsBorrowed(Item item) throws Exception{
		if(item == null)
			throw new Exception("Item inválido");
		
		if(myItemsBorrowed.contains(item))
			throw new Exception("O item já está na lista");
		
		myItemsBorrowed.add(item);
	}
	
	public ArrayList<Item> getMyItemsBorrowed(){
		return myItemsBorrowed;
	}
	
	public void addMyRequestedItems(Item item) throws Exception{
		if(item == null)
			throw new Exception("Item inválido");
		
		if(myItemsBorrowed.contains(item))
			throw new Exception("O item já está na lista");
		
		myRequestedItems.add(item);
	}
	
	public ArrayList<Item> getMyRequestedItems(){
		return myRequestedItems;
	}
	
	public void addMyReturnedItems(Item item) throws Exception{
		if(item == null)
			throw new Exception("Item inválido");
		
		myReturnedItems.add(item);
	}
	
	public ArrayList<Item> getMyReturnedItems(){
		return myReturnedItems;
	}
	
	public void addItemsReturnedToOwner(Item item) throws Exception{
		if(item == null)
			throw new Exception("Item inválido");
		
		if(itemsReturnedToOwner.contains(item))
			throw new Exception("O item já foi devolvido");
		
		itemsReturnedToOwner.add(item);
	}
	
	public ArrayList<Item> getItemsReturnedToOwner(){
		return itemsReturnedToOwner;
	}
	
	public void addMyOffTopicMessages(Message message) throws Exception{
		if(message == null)
			throw new Exception("Mensagem inválida");
		
		offTopicMessages.add(message);
	}
	
	public ArrayList<Message> getMyOffTopicMessages(){
		return offTopicMessages;
	}
	
	public void addMyNegotiationMessages(Message message) throws Exception{
		if(message == null)
			throw new Exception("Mensagem inválida");
		
		negotiationMessages.add(message);
	}
	
	public ArrayList<Message> getMyNegotiationMessages(){
		return negotiationMessages;
	}
	
	public void addMyAllMessages(Message message) throws Exception{
		if(message == null)
			throw new Exception("Mensagem inválida");
		
		allMessages.add(message);
	}
	
	public ArrayList<Message> getMyAllMessages(){
		return allMessages;
	}
	
	public void addMyDesiredItems(Item desiredItem, User user) throws Exception{
		if(desiredItem == null)
			throw new Exception("Item inválido");
		
		if(user == null)
			throw new Exception("Usuário inválido");
		
		if(desiredItems.containsKey(desiredItem)){
			desiredItems.get(desiredItem).add(user);
		}
		else{
			ArrayList<User> users = new ArrayList<User>();
			users.add(user);
			desiredItems.put(desiredItem, users);
		}
	}
	
	public Map<Item, ArrayList<User>> getMyDesiredItems(){
		return desiredItems;
	}
	
	public void addMyReturnRequests(Item returnRequestItem) throws Exception{
		if(returnRequestItem == null)
			throw new Exception("Item inválido");
		
		if(myReturnRequests.contains(returnRequestItem))
			throw new Exception("Devolução já requisitada");
		
		myReturnRequests.add(returnRequestItem);
	}
	
	public ArrayList<Item> getMyReturnRequests(){
		return myReturnRequests;
	}
	
	public void addToMyHistoric(String historic) throws Exception{
		if(historic == null || historic.equals("")){
			throw new Exception("Históric inválido");
		}
		
		myHistoric.add(0, historic);
	}
	
	public ArrayList<String> getMyHisotric(){
		return myHistoric;
	}
}